package com.zzl.alligator.management.vote.controller;

import com.zzl.alligator.management.vote.domain.VoteOption;
import com.zzl.alligator.management.vote.service.IVoteOptionService;
import com.zzl.alligator.framework.base.controller.BaseController;
import com.zzl.alligator.framework.support.web.vo.JsonResult;
import com.zzl.alligator.common.util.ExcelUtils;
import com.zzl.alligator.common.enums.BusinessTypeEnum;
import com.zzl.alligator.framework.aspectj.annotation.Log;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 投票选项(VoteOption)控制层
 *
 * @author zhouziliang
 * @since 2020-07-22
 */
@RestController
@RequestMapping("/voteOption")
public class VoteOptionController extends BaseController {
    
    /**
     * 服务对象
     */
    @Autowired
    private IVoteOptionService voteOptionService;

    /**
     * 分页查询所有数据
     *
     * @param voteOption 查询实体
     * @return 所有数据
     */
    @GetMapping
    public JsonResult findList(VoteOption voteOption) {
        startPage();
        List<VoteOption> voteOptionList = voteOptionService.findVoteOptionList(voteOption);
        return JsonResult.success(getDataTable(voteOptionList));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param optionId 主键
     * @return 单条数据
     */
    @GetMapping("/{optionId}")
    public JsonResult findById(@PathVariable Long optionId) {
        return JsonResult.success(voteOptionService.findVoteOptionById(optionId));
    }

    /**
     * 新增数据
     *
     * @param voteOption 实体对象
     * @return 新增结果
     */
    @PostMapping
    @Log(title = "新增投票选项", businessType = BusinessTypeEnum.INSERT)
    public JsonResult add(@RequestBody VoteOption voteOption) {
        return toAjax(this.voteOptionService.insertVoteOption(voteOption));
    }

    /**
     * 修改数据
     *
     * @param voteOption 实体对象
     * @return 修改结果
     */
    @PutMapping
    @Log(title = "更新投票选项", businessType = BusinessTypeEnum.UPDATE)
    public JsonResult update(@RequestBody VoteOption voteOption) {
        return toAjax(this.voteOptionService.updateVoteOption(voteOption));
    }

    /**
     * 删除数据
     *
     * @param optionId 主键
     * @return 删除结果
     */
    @DeleteMapping("/{optionId}")
    @Log(title = "删除投票选项", businessType = BusinessTypeEnum.DELETE)
    public JsonResult delete(@PathVariable Long optionId) {
        return toAjax(this.voteOptionService.deleteById(optionId));
    }
    
    /**
     * 导出数据
     * 
     * @param voteOption 条件
     * @return 导出文件路径
     */
    @GetMapping("/export")
    @Log(title = "导出投票选项", businessType = BusinessTypeEnum.EXPORT)
    public JsonResult export(VoteOption voteOption) {
        List<VoteOption> list = voteOptionService.findVoteOptionList(voteOption);
        ExcelUtils<VoteOption> util = new ExcelUtils<>(VoteOption.class);
        return util.exportExcel(list, "投票选项数据");
    }
}